package it.polimi.reTraveldream.eao;

import java.util.ArrayList;
import java.util.List;

import it.polimi.reTravelDream.dto.CityDTO;
import it.polimi.reTravelDream.eao.CityEAOLocal;
import it.polimi.reTraveldream.entities.City;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;




@Stateless
public class CityEAO implements CityEAOLocal{

	@PersistenceContext
    private EntityManager em;
	
	@Override
	public void addCity(CityDTO cityToAdd) {
		City newCity= new City(cityToAdd);
		em.persist(newCity);		
	}

	@Override
	public List<CityDTO> getAllCity() {
		List<City> resultEntities= em.createNamedQuery(City.FIND_ALL, City.class)
                .getResultList();
		 List<CityDTO> resultDTO= new ArrayList<CityDTO>();
		 for (City c: resultEntities){
			 resultDTO.add(convertToDTO(c));
		 }
		 return resultDTO;
	}
	
	private CityDTO convertToDTO(City city){
		CityDTO cityDTO=new CityDTO();
		
		cityDTO.setCity(city.getCity());
		cityDTO.setId(city.getId());
		return cityDTO;
	}

	@Override
	public void removeCity(CityDTO cityToRemove) {
		City city=em.find(City.class, cityToRemove.getId());
		em.remove(city);		
	}

	@Override
	public void editCity(CityDTO cityToEdtit) {
		// TODO Auto-generated method stub
		
	}
}
